/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author luosisi
 * Date: 2010-7-5 上午10:59:36
 */
package com.ccxe.manage.dao.impl;

import java.sql.ResultSet;

import com.ccxe.manage.dao.UserRoleDAO;
import com.ccxe.manage.db.CcxDB;
import com.ccxe.manage.db.DBFactory;
import com.ccxe.manage.model.UserRoleBean;

/**
 * @author luosisi
 * 
 */
public class UserRoleDaoImpl implements UserRoleDAO {

	public int addUserRole(UserRoleBean userRoleBean) {

		return 0;
	}

	public String getDeleteByRoleSql() {
		return "delete from  C_user_role  where user_id=?";
	}

	public int deleteUserRole(String id) {
		CcxDB ccxDB = null;
		int rs = 0;
		try {
			String sqlKey = getDeleteByRoleSql();
			String[] str = { id };
			ccxDB = DBFactory.getInstance();
			rs = ccxDB.executeUpdate(sqlKey, str);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ccxDB != null)
					ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	public UserRoleBean getById(String value, String id) {
		CcxDB ccxDB = null;
		ResultSet rs = null;
		UserRoleBean ur = null;
		try {
			if (value != null) {
				String sql = "select * from C_user_role where 1=1";
				if (value.equals("user")) {
					sql += " and USER_ID=?";
				} else if (value.equals("role")) {
					sql += " and ROLE_ID=?";
				}
				String[] items = new String[1];
				items[0] = id;
				ccxDB = DBFactory.getInstance();
				rs = ccxDB.executeQuery(sql, items);
				if (rs.next()) {
					ur = new UserRoleBean();
					ur.setRoleID(rs.getString("ROLE_ID"));
					ur.setUserID(rs.getString("USER_ID"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ccxDB != null)
					ccxDB.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ur;
	}

	public int updateUserRole(UserRoleBean userRoleBean) {

		return 0;
	}

}
